// JavaScript Document
var xmlHttp;
var xmlHttpEmail;
var xmlHttpUser;
var xmlHttpMaXN;
var xmlHttpOldPass;

var iuser = 0;
var iemail = 0;
var ima = 0;
var ipass = 0;
var i2pass = 0;
var ioldpass = 0;

function getAllFieldByUser($userid)
{	
	xmlHttp = getXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	
	var url = "../common/kt_dangky.php";
	url += "?LinhVucUser=" + 1;
	url += "&UserId" + $userid;
	url += "&t=" + new Date().getTime();

	xmlHttp.onreadystatechange = handleResponseLinhVuc;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function handleResponseLinhVuc()
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{		
		var kq = xmlHttp.responseText;
		document.getElementById('tdLinhVuc').innerHTML = kq;
	}
}

function KTPass1_Pass2()
{
	var pass1 = document.getElementsByName("txtPassword1")[0];
	var pass2 = document.getElementsByName("txtPassword2")[0];
		
	if(pass1.value != pass2.value)
	{
		baoloi("lbPassword2","* Mật khẩu phải giống nhau");
		i2pass = 0;
		return;
	}
	baoloi("lbPassword2","");
	i2pass = 1;

}

function getXmlHttpObject()
{
	if(window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

//cap nhat thong tin nguoi dung

function checkAllRightCapNhat()
{
	var mail = document.getElementsByName('txtEmail')[0].value;
	var pass1 = document.getElementsByName('txtPassword1')[0].value;
	var oldpass = document.getElementsByName('txtOldPassword')[0].value;

	KTEmailUpdate(mail);
	KTOldPass(oldpass);
	KTPassUpdate(pass1);
	KTPass1_Pass2();

	if(iemail == 1 && ipass == 1 && i2pass == 1 && ioldpass == 1)
	{	
		return 1;
	}

	document.getElementById('loadWait').style.display = "none";
	return 0;
}

function CapNhat()
{
	if(checkAllRightCapNhat() == 1)	
	{
		document.frmChangeUserInfo.submit();
	}
}

function KTCapNhat()
{
	checkAllRightCapNhat();
	document.getElementById('loadWait').style.display = "block";
	setTimeout('CapNhat()',3000);
}

function KTOldPass(oldpass)
{
	if(oldpass == '')
	{
		baoloi("lbOldPassword"," * Vui lòng nhập mật khẩu cũ!");
		ioldpass = 0;
		return;
	}
	else if(oldpass.length < 6)
	{
		baoloi("lbOldPassword","Mật khẩu cũ phải trên 5 kí tự.");
		ioldpass = 0;
		return;
	}
	
	xmlHttpOldPass = getXmlHttpObject();
	if(xmlHttpOldPass == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	var username = document.getElementsByName('txtUsername')[0].value;
	
	var url = "../common/kt_dangky.php";
	url += "?oldpass=" + oldpass;
	url += "&userName=" + username;
	url += "&t=" + new Date().getTime();
	xmlHttpOldPass.onreadystatechange = handleResponseOldPass;
	xmlHttpOldPass.open("GET",url,true);
	xmlHttpOldPass.send(null);
}

function handleResponseOldPass()
{
	if(xmlHttpOldPass.readyState == 4 && xmlHttpOldPass.status == 200)
	{
		document.getElementById('lbOldPassword').innerHTML = "";
		
		var kq = xmlHttpOldPass.responseText;
		if(kq == 1)
		{
			ioldpass = 1;
		}
		else
		{
			baoloi("lbOldPassword"," * Mật khẩu cũ không chính xác!");
			ioldpass = 0;
		}
	}
}

	
function KTEmailUpdate(mail)
{
	var str = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
	
	if(mail.length == 0)
	{
		baoloi("lbEmail"," * Vui lòng nhập email!");
		iemail = 0;
		return;
	}
	if(!mail.match(str))
	{ 
		baoloi("lbEmail","* Email không hợp lệ");
		iemail = 0;
		return;
	} 
		
	xmlHttpEmail = getXmlHttpObject();
	if(xmlHttpEmail == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	var username = document.getElementsByName('txtUsername')[0].value;
	
	var url = "../common/kt_dangky.php";
	url += "?UserName=" + username;
	url += "&Email=" + mail;
	url += "&t=" + new Date().getTime();

	xmlHttpEmail.onreadystatechange = handleResponseEmailUpdate;
	xmlHttpEmail.open("GET",url,true);
	xmlHttpEmail.send(null);	
}

function handleResponseEmailUpdate()
{
	document.getElementById('lbEmail').innerHTML = "";
	if(xmlHttpEmail.readyState == 4 && xmlHttpEmail.status == 200)
	{
		var kq = xmlHttpEmail.responseText;
		
		if(kq == -1)
		{
			baoloi('lbEmail',' * Email đã tồn tại.');
			iemail = 0;
		}
		else
		{	
			iemail = 1;
		}
	}
}


function KTPassUpdate(pass)
{
	if(pass.length == 0)
	{
		baoloi("lbPassword1","");
		ipass = 1;
		return;
	}
	else if(pass.length < 6)
	{
		baoloi("lbPassword1","Mật khẩu phải trên 5 kí tự.");
		ipass = 0;
		return;
	}
	baoloi("lbPassword1","");
	ipass = 1;
}


function baoloi(name,Loi)
{
	document.getElementById(name).innerHTML = "<font color='red'>" + Loi + "</font>";
}